/**
 * 聚合点
 * Created by kedong on 2017/7/28 0028.
 */
(function () {
    var Cluster = function (option) {
        // 聚合点图层
        var cluster_layer;
        // 点数据
        this.geoJson = option.geoJson;

        this.createSource = function (geojson) {
            return new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(geojson)
            });
        };
        var source;

        var clusterSource = new ol.source.Cluster({
            projection: 'EPSG:4326',
            distance: 270,
            source: source
        });

        var styleCache = {};
        var clusters = new ol.layer.Vector({
            source: clusterSource,
            style: function (feature) {
                var size = feature.get('features').length;

                var style = styleCache[size];
                if (!style) {
                    style = new ol.style.Style({
                        text: new ol.style.Text({
                            text: size == 1 ? '' : size.toString(),
                            fill: new ol.style.Fill({
                                color: '#fff'
                            })
                        })
                    });

                    var image;
                    if (size == 1) {
                        image = new ol.style.Icon({
                            anchor: [0.5, 1],
                            scale: 1,
                            src: ctx + '/static/images/map/marker-blue.png'
                        });
                    } else {
                        image = new ol.style.Circle({
                            radius: 15,
                            stroke: new ol.style.Stroke({
                                color: '#f0f'
                            }),
                            fill: new ol.style.Fill({
                                color: '#3399CC'
                            })
                        })
                    }
                    style.setImage(image);

                    styleCache[size] = style;
                }
                return style;
            }
        });

        clusters.setSource()
    }

    Cluster.prototype.addPoints = function (geojson) {
        this.geoJson
    }

})();